package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import model.BehandlingsTilstand;
import model.Lokation;
import model.Mellemvare;
import service.Service;

/**
 * @author Aske B. Vammen
 */
public class SetLokationDialog extends JDialog {
	private JPanel pnlBottomArea;
	private JButton btnVaelg;
	private JButton btnAnnuller;
	private JPanel pnlTopArea;
	private JPanel pnlLokationArea;
	private JScrollPane scrollPane;
	private JTable tblLedigeLokationer;
	
	private MainFrame owner;
	private Mellemvare mellemvare;

	public SetLokationDialog(MainFrame owner, Mellemvare mellemvare) {
		super(owner, "Opret ny \"Mellemvare\"", true);
		setTitle("V\u00E6lg Lokation");
		this.setSize(new Dimension(379, 310));
		this.setLocationRelativeTo(owner);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPnlBottomArea(), BorderLayout.SOUTH);
		getContentPane().add(getPnlTopArea(), BorderLayout.CENTER);
		this.mellemvare = mellemvare;
	}
	private SetLokationDialog getSLDialog() {
		return this;
	}
	private JPanel getPnlBottomArea() {
		if (pnlBottomArea == null) {
			pnlBottomArea = new JPanel();
			pnlBottomArea.add(getBtnVaelg());
			pnlBottomArea.add(getBtnAnnuller());
		}
		return pnlBottomArea;
	}
	private JButton getBtnVaelg() {
		if (btnVaelg == null) {
			btnVaelg = new JButton("V\u00E6lg");
			btnVaelg.addActionListener(new BtnBtnVaelgActionListener());
		}
		return btnVaelg;
	}
	private JButton getBtnAnnuller() {
		if (btnAnnuller == null) {
			btnAnnuller = new JButton("Annull\u00E9r");
			btnAnnuller.addActionListener(new BtnAnnullerActionListener());
		}
		return btnAnnuller;
	}
	private JPanel getPnlTopArea() {
		if (pnlTopArea == null) {
			pnlTopArea = new JPanel();
			pnlTopArea.setLayout(new BorderLayout(0, 0));
			pnlTopArea.add(getPnlLokationArea(), BorderLayout.CENTER);
		}
		return pnlTopArea;
	}
	private JPanel getPnlLokationArea() {
		if (pnlLokationArea == null) {
			pnlLokationArea = new JPanel();
			pnlLokationArea.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "V\u00E6lg Lokation:", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
			pnlLokationArea.setLayout(new GridLayout(0, 1, 0, 0));
			pnlLokationArea.add(getScrollPane());
		}
		return pnlLokationArea;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTblLedigeLokationer());
		}
		return scrollPane;
	}
	private JTable getTblLedigeLokationer() {
		if (tblLedigeLokationer == null) {
			Object[] oa = Service.getService().findLedigeLokationer().toArray();
			Lokation[] lo = new Lokation[oa.length];
			for (int i = 0; i < oa.length; i++) {
				lo[i] = (Lokation)oa[i];
			}
			tblLedigeLokationer = new LokationTable(
					lo,
					null);
			tblLedigeLokationer.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblLedigeLokationer.getTableHeader().setReorderingAllowed(false);
		}
		return tblLedigeLokationer;
	}
	private class BtnAnnullerActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			getSLDialog().dispose();
		}
	}
	private class BtnBtnVaelgActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (tblLedigeLokationer.getSelectedRowCount() == 1) {
				int selLoka = tblLedigeLokationer.convertRowIndexToModel(tblLedigeLokationer.getSelectedRow());
				NotCellEditableLokationDefaultTableModel nceldtm = (NotCellEditableLokationDefaultTableModel)tblLedigeLokationer.getModel();
				mellemvare.setLokation((Lokation)nceldtm.getLokationAt(selLoka));
				mellemvare.setBehandlingsTilstand(BehandlingsTilstand.UNDER_TOERRING);
				mellemvare.setAktivDelBehandlingsNummer(mellemvare.getAktivDelBehandlingsNummer() + 1);
				mellemvare.setToerringsStartTidpunkt(new GregorianCalendar());
				NotCellEditableMellemvareDefaultTableModel ncemdtm = ((NotCellEditableMellemvareDefaultTableModel)getOwner().getTblMellemvarer().getModel());
				int selMell = getOwner().getTblMellemvarer().convertRowIndexToModel(getOwner().getTblMellemvarer().getSelectedRow());
				ncemdtm.removeRow(selMell);
				getSLDialog().dispose();
			}
		}
	}
	@Override
	public MainFrame getOwner() {
		return (MainFrame)super.getOwner();
	}
}
